package info.erge;

import java.io.IOException;
import java.util.Vector;

import jakarta.websocket.OnClose;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;

@ServerEndpoint("/show")
public class LiveServer {
	private Session session; // 当前会话对象
	public static Vector<LiveServer> clients = new Vector<LiveServer>();
	
	@OnOpen
	public void onOpen(Session session) {
		this.session = session;
		clients.add(this);
		System.out.println("新连接来了");
	}
	
	@OnClose
	public void onClose() {
		clients.remove(this);
		System.out.println("连接关闭了");
	}
	
	@OnMessage
	public void onMessage(String message, Session session) {
		for (LiveServer client: clients) {
			try {
				client.session.getBasicRemote().sendText(message);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
